import { dateFormat, rmbFormat } from "../../utils";
const dirCategory = (el, binding) => {
    let {v,tpl} = binding.value, vals=[];
    for(let i=0;i<v.length;i++){
        vals.push(tpl[v[i]].name);
    }
    let s = vals.join(',');
    if(s.length>15){
        s = s.substring(0,15)+'...';
    }
    el.innerText = s;
},dirRackType = (el, binding) => {
    return binding.value == 0?el.innerText='仓库':el.innerText='货架';
},dirRmbShow = (el, binding) => {
    let v = binding.value === undefined ? 0 : binding.value;
    el.innerText = rmbFormat(v, 2)
}

const directives = {
    datetime:{
        mounted(el, binding) {
            if(undefined == binding.value){el.innerText = 'loading';return;}
            el.innerText = dateFormat(binding.value, 'Y-M-D h:m:s');
        },
        updated(el, binding) {
            el.innerText = dateFormat(binding.value, 'Y-M-D h:m:s');
        }
    },
    digitOnly:{
        mounted(el, binding){
            let input = el.querySelector('input');
            input.addEventListener('input', (e)=>{
            if(!/^\d$/.test(e.data)){
                input.value = input.value.replace(/[^\d]/g,'');
            }
            })
        }
    },
    category:{
        mounted:dirCategory,
        updated:dirCategory
    },
    racktype:{
        mounted:dirRackType,
        updated:dirRackType
    },
    rmb:{
        mounted:dirRmbShow,
        updated:dirRmbShow
    }
}

export default directives;
